package weibo4j;

import java.util.List;

import weibo4j.model.Paging;
import weibo4j.model.PostParameter;
import weibo4j.model.Tag;
import weibo4j.model.TagWapper;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;
import weibo4j.util.WeiboConfig;

public class Tags {
	/**
	 * @return list of the tags
	 * @throws WeiboException
	 *             when Weibo service or network is unavailable
	 * @version weibo4j-V2 1.0.1
	 * @see <a href="http://open.weibo.com/wiki/2/tags">tags</a>
	 * @since JDK 1.5
	 */
	public List<Tag> getTags(Weibo weibo, String uid) throws WeiboException {
		return Tag.constructTags(weibo.getClient().get(
				WeiboConfig.getValue("baseURL") + "tags.json",
				new PostParameter[] { new PostParameter("uid", uid) }));
	}

	/**
	 * @return list of the tags
	 * @throws WeiboException
	 *             when Weibo service or network is unavailable
	 * @version weibo4j-V2 1.0.1
	 * @see <a href="http://open.weibo.com/wiki/2/tags">tags</a>
	 * @since JDK 1.5
	 */
	public List<Tag> getTags(Weibo weibo, String uid, Paging page) throws WeiboException {
		return Tag.constructTags(weibo.getClient().get(
				WeiboConfig.getValue("baseURL") + "tags.json",
				new PostParameter[] { new PostParameter("uid", uid) }, page));
	}

	/**
	 * when Weibo service or network is unavailable
	 * 
	 * @version weibo4j-V2 1.0.1
	 * @see <a
	 *      href="http://open.weibo.com/wiki/2/tags/tags_batch">tags/tags_batch</a>
	 * @since JDK 1.5
	 */
	public TagWapper getTagsBatch(Weibo weibo, String uids) throws WeiboException {
		return Tag.constructTagWapper(weibo.getClient().get(
				WeiboConfig.getValue("baseURL") + "tags/tags_batch.json",
				new PostParameter[] { new PostParameter("uids", uids) }));
	}

	/**
	 * href="http://open.weibo.com/wiki/2/tags/suggestions">tags/suggestions</a>
	 * 
	 * @since JDK 1.5
	 */

	public List<Tag> getTagsSuggestions(Weibo weibo) throws WeiboException {
		return Tag.constructTags(weibo.getClient().get(WeiboConfig
				.getValue("baseURL") + "tags/suggestions.json"));
	}

	/**
	 * @version weibo4j-V2 1.0.1
	 * @see <a href="http://open.weibo.com/wiki/2/tags/create">tags/create</a>
	 * @since JDK 1.5
	 */
	public JSONArray createTags(Weibo weibo, String tags) throws WeiboException {
		return weibo.getClient().post(
				WeiboConfig.getValue("baseURL") + "tags/create.json",
				new PostParameter[] { new PostParameter("tags", tags) })
				.asJSONArray();
	}

	/**
	 * when Weibo service or network is unavailable
	 * 
	 * @version weibo4j-V2 1.0.1
	 * @throws JSONException
	 * @see <a href="http://open.weibo.com/wiki/2/tags/destroy">tags/destroy</a>
	 * @since JDK 1.5
	 */
	public JSONObject destoryTag(Weibo weibo, Integer tag_id) throws WeiboException {
		return weibo.getClient().post(
				WeiboConfig.getValue("baseURL") + "tags/destroy.json",
				new PostParameter[] { new PostParameter("tag_id", tag_id
						.toString()) }).asJSONObject();
	}

	/**
	 * when Weibo service or network is unavailable
	 * 
	 * @version weibo4j-V2 1.0.1
	 * @see <a
	 *      href="http://open.weibo.com/wiki/2/tags/destroy_batch">tags/destroy_batch</a>
	 * @since JDK 1.5
	 */
	public List<Tag> destroyTagsBatch(Weibo weibo, String ids) throws WeiboException {
		return Tag.constructTags(weibo.getClient().post(
				WeiboConfig.getValue("baseURL") + "tags/destroy_batch.json",
				new PostParameter[] { new PostParameter("ids", ids) }));
	}
}
